<template>
  <div class="editEeclareRecord">
    <van-nav-bar title="申报审核" left-arrow @click-left="$router.go(-1)" fixed />
    <van-cell
      title="当事人楼号"
      :value="datas.buildingCode"
      is-link
      @click="show = true"
      v-if="datas.applyMode === 2"
    />
    <van-cell title="楼号" :value="datas.buildingCode" is-link v-if="datas.applyMode === 1" />
    <van-cell
      title="当事人身份"
      :value="datas.applyIdentityName"
      is-link
      v-if="datas.applyMode === 1"
    />
    <van-cell
      title="当事人身份选择"
      :value="datas.applyIdentityName"
      is-link
      @click="clickApplyIdentityName"
      v-if="datas.applyMode === 2 "
    />
    <van-cell title="申报人姓名" :value="datas.applyName" is-link v-if="datas.applyMode === 1" />
    <van-cell
      title="申报人姓名"
      :value="datas.applyName"
      is-link
      @click="clickOtherName"
      v-if="datas.applyMode === 2"
    />
    <van-cell
      title="选择类型"
      v-if="datas.applyMode === 2"
      @click="click_type"
      is-link
      :value="datas.applyTypeName"
    />
    <van-cell title="类型" v-if="datas.applyMode === 1" :value="datas.applyTypeName" />
    <van-field
      v-model="message"
      rows="5"
      autosize
      readonly
      label="内容描述"
      type="textarea"
      :placeholder="datas.content"
    />
    <div class="integralDeclare-uploader">
      <img :src="datas.image" alt @click="seeImg" />
    </div>

    <div class="mine-out">
      <!-- 他人审核 -->
      <van-button
        color="linear-gradient(to right, #F6A039, #FC7C05)"
        @click="reject()"
        class="button"
        v-if="datas.applyMode === 2"
      >审批驳回</van-button>
      <van-button
        color="linear-gradient(to right, #F85944, #E53D26)"
        @click="save()"
        v-if="datas.applyMode === 2"
      >审批通过</van-button>
      <!-- 本人申报驳回 -->
      <van-button
        color="linear-gradient(to right, #F6A039, #FC7C05)"
        @click="rejectSelf()"
        class="button"
        v-if="datas.applyMode === 1"
      >审批驳回</van-button>
      <!-- 本人申报审核 -->
      <van-button
        color="linear-gradient(to right, #F85944, #E53D26)"
        @click="saveSelf()"
        v-if="datas.applyMode === 1"
      >审批通过</van-button>
    </div>
    <van-action-sheet v-model="show" :actions="otherbuildingNo" @select="otherOnSelect" @cancel="datas.buildingCode =''" cancel-text="取消" />
    <van-action-sheet v-model="show1" :actions="otheridentity" @select="otherOnSelect1"  @cancel="datas.applyIdentityName = ''" cancel-text="取消" />
    <!-- <van-action-sheet v-model="otherShowName" :actions="otherName" @select="otherOnSelectName" /> -->
     <van-action-sheet v-if="otherShowName" v-model="otherShowName" title="当事人姓名"  @cancel="datas.applyName =''" cancel-text="取消">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
        <van-list v-model="loading" :finished="finished"  @load="onLoad">
          <van-cell
            v-for="(item,index) in otherName"
            :value="item.idcardNo"
            :key="index"
            :title="item.username"
            @click="otherOnSelectName(item)"
          />
        </van-list>
      </van-pull-refresh>
    </van-action-sheet>
    <!-- 类型选择 -->
    <van-action-sheet v-model="otherShowType" :actions="identityType" @select="otherOnSelectType"  @cancel="datas.applyTypeName = ''" cancel-text="取消" />
  </div>
</template>

<script>
import constant from "@/controller/constant.js";
import integral from "../../controller/integral";
import { ImagePreview } from "vant";
// import { notEmpty } from "../workstation/emptyTest";
export default {
  data() {
    return {
      // otheridentityId: 0,
      // otherbuildingCodesId: "",
      otherbuildingNo: [], // 他人楼栋号
      otheridentity: [], // 他人当事人身份选择
      image: "",
      datas: {},
      partyName: "",
      message: "",
      show: false,
      show1: false,
      otherShowName: false,
      otherName: [],
      noContent: [{ name: "", subname: "" }],
      identityType: [],
      otherShowType: false,
      curPage: 1,
      pageSize: 10,
      isLoading: false,
      loading: false,
      finished: false,
    };
  },
  computed: {
    // disableds: function() {
    //   return (
    //     notEmpty(this.datas.applyName) &&
    //     notEmpty(this.datas.buildingCode) &&
    //     notEmpty(this.datas.applyIdentityName)
    //   );
    // }
  },
  created() {
    this.datas = JSON.parse(decodeURIComponent(this.$route.params.data));
  },
  mounted() {
    this.integral_building_neighborhoods();
    this.integral_identity_choose();
    this.getOtherOnSelectName();
    this.integral_mapNameAnndId(this.datas.applyIdentityId);
  },
  methods: {
    seeImg() {
      ImagePreview({
        images: [this.datas.image]
      });
    },

    // 选择类型
    integral_mapNameAnndId(data) {
      let id = {
        applyIdentityId: data
      };
      integral.integral_mapNameAndId(id).then(res => {
        if (res.code == "0") {
          if (res.datas.length !== 0) {
            let list = res.datas[0];
            for (let key in list) {
              this.identityType.push({ name: list[key], id: key });
            }
          } else {
            this.datas.applyTypeName = "";
          }
        } else {
          if (res && res.msg) {
            this.$toast(res.msg);
          } else {
            this.$toast(constant.unusualMsg);
          }
        }
      });
    },
    otherOnSelectType(item) {
      this.datas.applyTypeName = item.name;
      this.datas.applyTypeId = item.id;
      this.otherShowType = false;
    },
    click_type() {
      this.otherShowType = false;
      if (this.datas.applyIdentityName === "") {
        this.$toast("请选择身份");
        return;
      } else {
        this.otherShowType = true;
      }
      if (this.identityType.length === 0) {
        this.otherShowType = false;
        this.$toast("暂无数据");
      } else {
        this.otherShowType = true;
      }
    },
        onRefresh() {
      setTimeout(() => {
        this.curPage += 1;
        this.isLoading = false;
        this.getOtherOnSelectName();
      }, 500);
    },
    onLoad() {
      this.isLoading = false;
      this.onRefresh();
    },
    // 他人姓名选择
    getOtherOnSelectName() {
      let data = {
        curPage: this.curPage,
        pageSize: this.pageSize,
         buildCode: this.datas.buildingCode,
        identityId: this.datas.applyIdentityId
      };
      integral.integral_builderUsers(data).then(res => {
        if (res.code === "0") {
          this.loading = true
          this.finished = false;
          if (
            (res.datas && res.datas.records.length === 0) ||
            res.datas === null
          ) {
            this.loading = false;
            this.isLoading = false;
            this.finished = true;
            return;
          }
          this.$nextTick(() => {
            this.loading = false;
            this.isLoading = false;
          });
          if (res.datas.records.length < this.pageSize) {
            this.finished = true;
          }
          if (
            (res.datas && res.datas.records.length !== 0) ||
            res.datas !== null
          ) {
            let otherNameList = res.datas.records;
            otherNameList.forEach(item => {
              item.name = item.username;
              item.subname = item.idcardNo;
            });
            this.otherName = [...this.otherName, ...res.datas.records];
            
          }
        } else {
          if (res && res.msg) {
            this.$toast(res.msg);
          } else {
            this.$toast(constant.unusualMsg);
          }
        }
      });
    },
    // getOtherOnSelectName() {
    //   let data = {
    //     buildCode: this.datas.buildingCode,
    //     identityId: this.datas.applyIdentityId
    //   };
    //   integral.integral_builderUsers(data).then(res => {
    //     if (res.code === "0") {
    //       if (res.datas && res.datas.length !== 0) {
    //         let otherNameList = res.datas;
    //         otherNameList.forEach(item => {
    //           item.name = item.username;
    //           item.subname = item.idcardNo;
    //         });
    //         this.otherName = otherNameList;
    //         // for (let key in otherNameList) {
    //         //   console.log(key, "key");
    //         //   this.otherName.push({ name: otherNameList[key], id: key });
    //         // }
    //       } else {
    //         this.otherShowName = false;
    //       }
    //     }
    //   });
    // },
    //     // 姓名选择
    // clickApplyName() {
    //   this.otherShowName = false;
    //   if (this.otherName.length === 0) {
    //     this.$toast("暂无数据");
    //   } else {
    //     this.otherShowName = true;
    //     this.getOtherOnSelectName();
    //   }
    //   // if (
    //   //   this.datas.buildingCode === "" ||
    //   //   this.datas.applyIdentityName === ""
    //   // ) {
    //   //   this.otherName = this.noContent;
    //   // } else {
    //   //   this.getOtherOnSelectName();
    //   // }
    // },
     // 姓名选择
        clickOtherName() {
      this.otherShowName = false;
      if (this.otherName.length === 0) {
        this.$toast("暂无姓名");
      } else {
        this.curPage = 1;
        this.otherName = [];
        this.otherShowName = true;
        this.getOtherOnSelectName();
      }
      if (this.datas.applyIdentityName === null) {
        this.$toast("请选择身份");
      }
      // if (this.otherbuildingNoValue === "" || this.otheridentityValue === "") {
      //   this.otherName = this.noContent;
      // } else {
      //   this.getOtherOnSelectName();
      // }
    },
    // 姓名选择
    otherOnSelectName(item) {
      this.datas.applyUserId = item.id;
      this.datas.applyName = item.name;
      this.otherShowName = false;
    },
    // 审核
    save() {
      // this.$router.push()({
      //   name: "declareRecord"
      // });
      let data = {
        applyId: this.datas.applyId,
        applyName: this.datas.applyName,
        buildingCode: this.datas.buildingCode,
        neighborhoodId: this.datas.neighborhoodId,
        applyIdentityId: this.datas.applyIdentityId,
        applyUserId: this.datas.applyUserId,
        applyTypeId: Number(this.datas.applyTypeId)
          ? Number(this.datas.applyTypeId)
          : null,
        buildAudit: 2 // 楼栋长审核状态 1：审核中 2：审核通过 3：审核驳回
      };
      integral.integral_edit_builder(data).then(res => {
        if (res.code === "0") {
          this.$toast("审核通过");
          this.$router.go(-1);
        } else {
          this.$toast("审核失败");
        }
      });
    },
    // 驳回
    reject() {
      let data = {
        applyId: this.datas.applyId,
        applyName: this.datas.applyName,
        buildingCode: this.datas.buildingCode,
        neighborhoodId: this.datas.neighborhoodId,
        applyIdentityId: this.datas.applyIdentityId,
        applyUserId: this.datas.applyUserId,
        applyTypeId: Number(this.datas.applyTypeId)
          ? Number(this.datas.applyTypeId)
          : null,
        buildAudit: 3 // 楼栋长审核状态 1：审核中 2：审核通过 3：审核驳回
      };
      integral.integral_edit_builder(data).then(res => {
        if (res.code === "0") {
          this.$toast("已驳回");
          this.$router.go(-1);
        } else {
          this.$toast("驳回失败");
        }
      });
    },
    // 本人审核
    saveSelf() {
      let data = {
        applyId: this.datas.applyId,
        applyName: this.datas.applyName,
        buildingCode: this.datas.buildingCode,
        neighborhoodId: this.datas.neighborhoodId,
        applyIdentityId: this.datas.applyIdentityId,
        buildAudit: 2 // 楼栋长审核状态 1：审核中 2：审核通过 3：审核驳回
      };
      integral.integral_edit_builder(data).then(res => {
        if (res.code === "0") {
          this.$toast("审核通过");
          this.$router.go(-1);
        } else {
          this.$toast("审核失败");
        }
      });
    },
    // 本人驳回
    rejectSelf() {
      let data = {
        applyId: this.datas.applyId,
        applyName: this.datas.applyName,
        buildingCode: this.datas.buildingCode,
        neighborhoodId: this.datas.neighborhoodId,
        applyIdentityId: this.datas.applyIdentityId,
        buildAudit: 3 // 楼栋长审核状态 1：审核中 2：审核通过 3：审核驳回
      };
      integral.integral_edit_builder(data).then(res => {
        if (res.code === "0") {
          this.$toast("已驳回");
          this.$router.go(-1);
        } else {
          this.$toast("驳回失败");
        }
      });
    },
    // 他人申报楼栋号
    integral_building_neighborhoods() {
      let id = this.datas.neighborhoodId;
      integral.integral_building_neighborhoods(id).then(res => {
        if (res.code == "0") {
          if (res.datas.length !== 0) {
            let otherBuildingList = res.datas;
            otherBuildingList.forEach(item => {
              item.name = item.buildingCode;
            });
            this.otherbuildingNo = otherBuildingList;
          }
        } else {
          if (res.msg === "") {
            this.$toast("网络异常");
          } else {
            this.$toast(res.msg);
          }
        }
      });
    },
    // 他人申报楼号
    otherOnSelect(item) {
      this.datas.buildingCode = item.name;
      // this.otherbuildingCodesId = item.buildingCode;
      this.show = false;
      if (this.datas.applyIdentityId !== "") {
        this.datas.applyName = "";
       this.curPage = 1;
        this.getOtherOnSelectName();
      }
    },
    // 他人申报当事人身份选择
    otherOnSelect1(item) {
      this.datas.applyIdentityName = item.name;
      this.datas.applyIdentityId = item.id;
      this.show1 = false;
        this.curPage = 1;
      this.getOtherOnSelectName();
      this.integral_mapNameAnndId(item.id);
      // if (this.datas.buildingCode !== "") {
      //   this.getOtherOnSelectName();
      // }
    },
    // 选择他人身份
    clickApplyIdentityName() {
      this.datas.applyTypeName = "";
      this.datas.applyName = "";
      this.otherName = [];
      this.identityType = [];
      this.show1 = true;
    },
    // 当事人身份选择
    integral_identity_choose() {
      integral.integral_identity_choose().then(res => {
        if (res.code == "0") {
          if (res.datas.length !== 0) {
            this.otheridentity = res.datas;
          }
        } else {
          if (res && res.msg) {
            this.$toast(res.msg);
          } else {
            this.$toast(constant.unusualMsg);
          }
        }
      });
    },
    onClickRight() {
      this.$router.push({ name: "declareRecord" });
    }
  }
};
</script>

<style lang="scss" scoped>
.editEeclareRecord {
  height: 100%;
  background: #fff;
  .integralDeclare-uploader {
    width: 100px;
    height: 100px;
    padding: 20px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .mine-out {
    width: 100%;
    position: fixed;
    bottom: 0;
    left: 0;
    button {
      width: 50%;
      height: 80%;
      margin: auto;
      font-family: PingFangSC-Medium;
      font-size: 18px;
      color: #fff;
      letter-spacing: 0;
      text-align: center;
      display: inline-block;
    }
    .button {
      width: 50%;
      height: 80%;
      margin: auto;
      display: inline-block;
      font-family: PingFangSC-Medium;
      font-size: 18px;
      color: #fff;
      letter-spacing: 0;
      text-align: center;
      background: #dc4630;
      margin-top: 5px;
    }
  }
  /deep/ .van-action-sheet__cancel, .van-action-sheet__item {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
